package com.my.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.my.demo.pojo.FamilyInformation;
import com.my.demo.pojo.PerIdentify;
import com.my.demo.pojo.Result;
import com.my.demo.service.FamilyInformationService;
import com.my.demo.service.IdentifyService;
import com.my.demo.service.PerIdentifyService;
import com.my.demo.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SearchGenealogyController {
  @Autowired
  FamilyInformationService familyInformationService;
  @Autowired
  PersonService personService;
  @Autowired
  IdentifyService identifyService;
  @Autowired
  PerIdentifyService perIdentifyService;
  @PostMapping("search_genealogy")
  @CrossOrigin
  public Result searchGenealogy(@RequestBody HashMap<String,String> map,
      HttpSession session){
    System.out.println("开始搜索");
    Result result = new Result();
    result.setMsg(null);
    result.setSuccess(false);

    //获取当前用户的所有家谱
    QueryWrapper<FamilyInformation> familyInformationQueryWrapper = new QueryWrapper<FamilyInformation>();
//        familyInformationQueryWrapper.eq("uploader",userId);
    List<FamilyInformation> familyInformationList = null;
    try{
//            familyInformationList = familyInformationService.list(familyInformationQueryWrapper);
      familyInformationList = familyInformationService.list(null);
    }catch (Exception exception){
      exception.printStackTrace();
    }
//        System.out.println("uploader:"+userId);
//        for (FamilyInformation familyInformation : familyInformationList) {
//            System.out.println(familyInformation);
//        }
    //根据搜索信息进行搜索
    //如果居住地不为空，对居住地进行查询
    //居住地
    String habitation = null;
    try {
      habitation = map.get("habitation");
    }catch (Exception exception){
      exception.printStackTrace();
    }
    System.out.println("habitation:"+habitation);
    if (habitation!= null && !habitation.equals("")){
      familyInformationQueryWrapper.like("residence",habitation);
    }
    //如果家谱谱序不为空，对家谱谱序进行查询
    String genealogy = null;
    try{
      genealogy = map.get("genealogy");
    }catch (Exception exception){
      exception.printStackTrace();
    }
    System.out.println("genealogy:"+genealogy);
    if (genealogy!= null && !genealogy.equals("")){
      familyInformationQueryWrapper.like("genealogy",genealogy);
    }

    //如果家谱名不为空，对家谱名进行查询
    String genealogyName = null;
    try{
      genealogyName = map.get("genealogyName");
    }catch (Exception exception){
      exception.printStackTrace();
    }
    System.out.println("genealogyName:"+genealogyName);
    if (genealogyName!= null && !genealogyName.equals("")){
      familyInformationQueryWrapper.like("name",genealogyName);
    }

    try {
      if (familyInformationQueryWrapper.isEmptyOfWhere()){
        System.out.println("查询条件为空");
        familyInformationList = familyInformationService.list();
      }
      else {
        familyInformationList = familyInformationService.list(familyInformationQueryWrapper);
      }
    }catch (Exception exception){
      exception.printStackTrace();
    }

    //如果没有满足条件的家谱，那么直接返回即可
    //如果有满足条件的家谱，则存下其家谱ID
    if (familyInformationList.isEmpty()){
      System.out.println("没有满足条件的家谱");
    }else {
      System.out.println("此处输出满足条件的家谱的信息");
      /*
       * 往家谱中放的：第一张图片，家谱名，祖籍，谱序等信息
       * */
      List<Map> resultList = new ArrayList<>();
      QueryWrapper<PerIdentify> pictureQueryWrapper = new QueryWrapper<>();
      for (FamilyInformation familyInformation : familyInformationList) {
        Map<String,Object> mapMed = new HashMap<>();
        mapMed.put("genealogyName",familyInformation.getName());
        mapMed.put("household",familyInformation.getHousehold());
        mapMed.put("habitation",familyInformation.getResidence());
        mapMed.put("genealogy",familyInformation.getGenealogy());
        pictureQueryWrapper.eq("family_archive_id",familyInformation.getFamilyArchiveId());
        pictureQueryWrapper.eq("page_number",1);
        PerIdentify picturePath = null;
        try {
          picturePath = perIdentifyService.getOne(pictureQueryWrapper);
        }catch (Exception exception){
          exception.printStackTrace();
        }
        if (picturePath!=null){
          mapMed.put("picturePath",picturePath.getImg());
        }
        pictureQueryWrapper.clear();
        resultList.add(mapMed);
        System.out.println(familyInformation);
      }
      result.setSuccess(true);
      result.setDetail(resultList);
      result.setMsg("查询成功");
    }
    return result;
  }
}
